package com.example.chapter3_1.mapper;

import com.example.chapter3_1.entity.Card;
import com.example.chapter3_1.entity.Student;
import com.example.chapter3_1.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StuMapper {
    @Insert("insert into tb_student(no,name,sex,phone) values(#{no},#{name}#{sex},#{phone})")
    int creat(Student student);

    @Select("select * from tb_student where name like concat('%',#{name},'%')")
    List<Student> getByName(String name);

    @Result(property = "card",column = "no",
            one = @One(select = "com.rgzn.chapter3_1.dao.StudentMapper.getCardByNo"))
    @Select("select * from  tb_student where  name like concat('%',#{name},'%')")
    List<Student> getUsersByName(String name);

    @Select("select * from card where cardNo = #{cardNo}")
    Card getCardByNo(String cardNo);

    @Select({
            "<script>",
            "select * from tb_student where 1=1",
            "<if test='name!=null'>",
            " and name = #{name}",
            "</if>",
            "<if test='no!=null'>",
            " and no = #{no}",
            "</if>",
            "</script>"
    })
    List<Student> getUsersByNameOrNumber(String name,String no);

    @Result(property = "card",column = "no",
            one = @One(select = "com.example.shiyan.mapper.StuMapper.getCardByCardNo"))

    @Select({
            "<script>",
            "select * from tb_student where 1=1",
            "<if test='name!=null'>",
            " and name like concat('%',#{name},'%')",
            "</if>",
            "<if test='no!=null'>",
            " and no like concat('%',#{name},'%')",
            "</if>",
            "</script>"
    })
    List<Student> getByNameOrSno(String name,String no);

    @Select("select * from  card where  cardNo =#{cardNo}")

    Card getCardByCardNo(String cardNo);

}
